const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const { DefinePlugin } = require('webpack')

module.exports = (env) => {
    console.log(env);
    const node_env = env.production ? 'production' : 'development';
    return {
        entry: './src/index.js',
        output: {
            filename: 'boundle.[contenthash:8].js',
            path: path.resolve(__dirname, './dist')
        },
        resolve: {
            extensions: ['.js', '.json', '.ts', '.vue'],
            alias: {
                '@': path.resolve(path.join(__dirname, 'src'))
            }
        },
        mode: 'development',
        devtool: 'source-map',
        target: 'web',
        devServer: {
            port: 3000,
            static: {
                directory: path.join(__dirname, 'public'),
            },
            historyApiFallback: true
        },
        module: {
            rules: [
                {
                    test: /\.css$/,
                    use: [
                        'style-loader',
                        'css-loader',
                        'postcss-loader'
                    ]
                },
                {
                    test: /\.less$/,
                    use: [
                        'style-loader',
                        'css-loader',
                        'postcss-loader',
                        'less-loader'
                    ]
                },
                {
                    test: /\.vue$/,
                    use: ['vue-loader']
                },
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    use: ['babel-loader']
                }
            ]
        },
        plugins: [
            new CleanWebpackPlugin(),
            new HtmlWebpackPlugin({
                template: './public/index.html'
            }),
            new DefinePlugin({
                NODE_ENV: 123,
                'process.env.NODE_ENV': JSON.stringify(node_env)
            }),
            new VueLoaderPlugin()
        ]
    }
}